package com.hnichr.stop.message.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.hnichr.share.IOStatusShare;
import com.hnichr.stop.message.MessageArrivedHandle;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

@Component
public class IOStatusMessageArrivedHandle implements MessageArrivedHandle {
    private static final Logger logger = LoggerFactory.getLogger(IOStatusMessageArrivedHandle.class);

    @Override
    public boolean support(String topic, String message) {
        return "b956ba98-988cd83e/device/message/down/get_io_status/reply".equals(topic);
    }

    @Override
    public void handle(String topic, String message) {
        try{
            JSONObject jsonObject = JSON.parseObject(message);
            JSONObject payload = (JSONObject)jsonObject.get("payload");
            JSONObject body = (JSONObject)payload.get("body");
            synchronized (IOStatusShare.getInstance()){
                IOStatusShare.getInstance().setStatus(body.getString("status"));
                IOStatusShare.getInstance().setGpio(body.getString("gpio"));
                IOStatusShare.getInstance().notifyAll();
            }
        }
        catch (Exception e){
            logger.error("IOStatusMessageArrivedHandle报错",e);
        }


    }
}
